package com.erp.erp_servers.ShipmentPlan.imp;

import com.erp.erp_dao.ShipmentPlan.IShipmentPlanProductDAO;
import com.erp.erp_entitys.Purchase.ExportVo;
import com.erp.erp_entitys.Purchase.PurchaseProductVO;
import com.erp.erp_entitys.ShipmentPlan.ShipmentModel;
import com.erp.erp_entitys.ShipmentPlan.ShipmentPlanProduct;
import com.erp.erp_servers.ShipmentPlan.IShipmentPlanProductService;
import com.erp.utils.WordUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImpShipmentPlanProductService implements IShipmentPlanProductService {

    @Autowired
    private IShipmentPlanProductDAO productDAO;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id,String type) {
        if("p".equals(type)){//删除产品
            //删除产品前先删除部件
            ShipmentPlanProduct shipmentPlanProduct = productDAO.selectByPrimaryKey(id);
            int num= productDAO.deleteByProductId(shipmentPlanProduct.getProductId());
            return productDAO.deleteByPrimaryKey(id);
        }else{
            //删除配件
            return productDAO.deleteByPrimaryKey(id);
        }

    }

    @Override
    public int insert(ShipmentPlanProduct record) {
        return productDAO.insert(record);
    }

    @Override
    public int insertSelective(ShipmentPlanProduct record) {
        return productDAO.insertSelective(record);
    }

    @Override
    public ShipmentPlanProduct selectByPrimaryKey(Integer id) {
        return productDAO.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ShipmentPlanProduct record) {
        return productDAO.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ShipmentPlanProduct record) {
        return productDAO.updateByPrimaryKey(record);
    }

    @Override
    public List<ShipmentPlanProduct> selectProductBySpId(Integer spId) {
        return productDAO.selectProductBySpId(spId);
    }

    @Override
    public List<ShipmentPlanProduct> selectProductByShipmentPlanId(Integer spId) {
        return productDAO.selectProductByShipmentPlanId(spId);
    }

    @Override
    public int deleteByIdList(List<Integer> idList) {
        return productDAO.deleteByIdList(idList);
    }

    @Override
    public PageInfo<ExportVo> selectExportByList(ExportVo vo) {
        List<ExportVo> list = productDAO.selectExportByList(vo);
        PageInfo pageInfo = new PageInfo(list);
        if(!CollectionUtils.isEmpty(list)){
            pageInfo.setList(list);
            pageInfo.setTotal(list.size());
            return pageInfo;
        }
        return pageInfo;
    }

    @Override
    public PageInfo<ShipmentPlanProduct> selectExportProductByExIds(List<Integer> idList) {
        List<ShipmentPlanProduct> list = productDAO.selectExportProductByExIds(idList);
        PageInfo pageInfo = new PageInfo(list);
        if(!CollectionUtils.isEmpty(list)){
            pageInfo.setList(list);
            pageInfo.setTotal(list.size());
            return pageInfo;
        }
        return pageInfo;
    }

    /**
     * 根据外销合同号查询采购合同
     *
     * @param salesContractNo
     * @return
     */
    @Override
    public List<PurchaseProductVO> getPurchaseByExportContractNo(String salesContractNo) {

        return productDAO.getPurchaseByExportContractNo(salesContractNo);
    }

    @Override
    public Map<String, Object> selectShipmentByCode(String code) {

        List<ShipmentModel> shipmentModels = productDAO.selectShipmentByCode(code);
        Map<String, Object> map=new HashMap<>();
        ShipmentModel sm = shipmentModels.get(0);

        ///////////////////
        map.put("startPlace2",sm.getStartPlace2()==null?"":sm.getStartPlace2());
        map.put("priceTerm2",sm.getPriceTerm2()==null?"":sm.getPriceTerm2());
        map.put("proFinishDate",sm.getProFinishDate()==null?"":sm.getProFinishDate());
        String imageBase64String = WordUtils.getImageStr("I:/bigdate/Tomcat 9.0/webapps/erp/statics/images/report/suplier.jpg");
        map.put("brand",imageBase64String);;
        map.put("transMode2",sm.getTransMode2()==null?"":sm.getTransMode2());
        map.put("boxType",sm.getBoxType()==null?"":sm.getBoxType());
        map.put("declareCompany",sm.getDeclareCompany()==null?"":sm.getDeclareCompany());

        List<ShipmentModel> list=new ArrayList<>();

        String ship="";
        boolean tf=false;
        String obj="";
        for(int i=0;i<shipmentModels.size();i++){
            ShipmentModel sml=shipmentModels.get(i);
            sml.setDeclareCompany(sml.getDeclareCompany()==null?"":sml.getDeclareCompany());

           sml.setTrayNumber(sml.getTrayNumber()==null?"":sml.getTrayNumber());
           sml.setTrayNumber(subZeroAndDot(""+Math.ceil(Double.parseDouble(sml.getNumberOfPiece()))));

          sml.setTrayCapacity(sml.getTrayCapacity()==null?"":sml.getTrayCapacity());
          sml.setTrayCapacity(subZeroAndDot(""+Math.ceil(Double.parseDouble(sml.getTrayCapacity()))));

            sml.setQty(sml.getQty()==null?"":sml.getQty());
             sml.setQty(subZeroAndDot(""+Math.ceil(Double.parseDouble(sml.getQty()))));

           sml.setNumberOfPiece(sml.getNumberOfPiece()==null?"":sml.getNumberOfPiece());
           sml.setNumberOfPiece(subZeroAndDot(""+Math.ceil(Double.parseDouble(sml.getNumberOfPiece()))));


           sml.setAmount(sml.getAmount()==null?"":sml.getAmount());
            sml.setAmount(subZeroAndDot( ""+Math.ceil(Double.parseDouble(sml.getAmount()))));

            sml.setEnglishDesc(sml.getEnglishDesc()==null?"":sml.getEnglishDesc());
            sml.setPurchaseNo(sml.getPurchaseNo()==null?"":sml.getPurchaseNo());
            sml.setSpecNo3(sml.getSpecNo3()==null?"":sml.getSpecNo3());
            sml.setEndPlace2(sml.getEndPlace2()==null?"":sml.getEndPlace2());
            sml.setStartPlace2(sml.getStartPlace2()==null?"":sml.getStartPlace2());
            sml.setProductNo2(sml.getProductNo2()==null?"":sml.getProductNo2());
           sml.setShipmentNo(sml.getShipmentNo()==null?"":sml.getShipmentNo());
            sml.setCompanyCode2(sml.getCompanyCode2()==null?"":sml.getCompanyCode2());

          if(obj.equals("")){
              sml.setStar("<w:vMerge w:val='restart'/>");
              sml.setEnd("1");//不结束
          }else if(obj.equals(sml.getCompanyCode2())){
              sml.setEnd("1");
              sml.setStar("1");
          }else{
              sml.setStar("0");
              sml.setEnd("1");
              ShipmentModel shipmentModel = shipmentModels.get(i-1);
              shipmentModel.setEnd("0");
          }
          if(i==shipmentModels.size()-1){
              sml.setEnd("0");
          }
            obj=sml.getCompanyCode2();

//出运计划
            if(ship.equals("")){

                sml.setShipstar("<w:vMerge w:val='restart'/>");
                sml.setShipend("1");//不结束
            }else if(ship.equals(sml.getShipmentNo())){
                sml.setShipend("1");
                sml.setShipstar("1");
            }else{
                sml.setShipstar("0");
                sml.setShipend("1");
                ShipmentModel shipmentModel = shipmentModels.get(i-1);
                shipmentModel.setShipend("0");
            }
            if(i==shipmentModels.size()-1){
                sml.setShipend("0");
            }
            ship=sml.getShipmentNo();


        }




        map.put("shipmentModels" ,shipmentModels);
        return map;
    }
    /**
     * 使用java正则表达式去掉多余的.与0
     * @param s
     * @return
     */
    public  String subZeroAndDot(String s){
        if(s.indexOf(".") > 0){
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }
}
